import 'dart:typed_data';

import 'package:binary/binary.dart';
import 'package:inmotion_protocol/common/compact.dart';
import 'package:inmotion_protocol/common/product_model.dart';
import 'package:inmotion_protocol/common/product_version.dart';
import 'package:inmotion_protocol/lorin/bean/euc/euc_settings.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

class E20Settings extends EucSettings {
  static const int ridingModeNewbee = 0; // 骑行模式 - 新手模式
  static const int ridingModeNormal = 1; // 骑行模式 - 普通模式
  static const int ridingModeSport = 2; // 骑行模式 - 运动模式

  static const int lightEffectModeDisabled = 0; // 灯效模式 - 关闭
  static const int lightEffectModeBySpeed = 3; // 灯效模式 - 跟随速度
  static const int lightEffectModeAlwaysOn = 4; // 灯效模式 - 常亮

  Uint8 lightEffectMode = Uint8.zero; // 氛围灯模式
  Uint32 colorOfLightEffectBySpeed = Uint32.zero; // 氛围灯跟随音乐模式下，氛围灯颜色
  Uint32 colorOfLightEffectAlwaysOn = Uint32.zero; // 氛围灯跟随速度模式下，氛围灯样式

  E20Settings(ProductModel model, VersionWrapper version) : super(model, version);

  @override
  void initCompact() {
    compact = _E20SettingsDefault();
  }

  @override
  String toString() {
    return 'E20Settings{driveMode: $driveMode, comfortableDriveModePedalSensitivity: $comfortableDriveModePedalSensitivity, classicDriveModePedalSensitivity: $classicDriveModePedalSensitivity, '
        'ridingMode: $ridingMode, standbyTime: $standbyTime, speedingFeedback: $speedingFeedback, brakingFeedback: $brakingFeedback, '
        'liftedState: $liftedState, lightEffectMode: $lightEffectMode, colorOfLightEffectBySpeed: $colorOfLightEffectBySpeed,  colorOfLightEffectAlwaysOn: $colorOfLightEffectAlwaysOn'
        'lockState: $lockState, transportModeState: $transportModeState, loadDetect: $loadDetect, noLoadDetect: $noLoadDetect, lowBatteryMode: $lowBatteryMode, '
        'remainderRangeEstimate: $remainderRangeEstimate, assistBalance: $assistBalance, speedingBrakingFeedbackState: $speedingBrakingFeedbackState, '
        'lowBatterySafeMode: $lowBatterySafeMode}';
  }
}

class _E20SettingsDefault extends Compact<E20Settings> {
  @override
  void fromBytes(E20Settings output, Uint8ClampedList bytes, int offset) {
    if (bytes.length < offset + length()) {
      return;
    }
    offset += 4;
    offset += 4;
    output.limitSpeed = ByteUtil.bytes2Uint16Le(bytes, offset);
    offset += 2;
    offset += 2;
    offset += 2;
    output.pitchAngleZero = ByteUtil.bytes2Int16Le(bytes, offset);
    offset += 2;
    output.standbyTime = ByteUtil.bytes2Uint16Le(bytes, offset);
    offset += 2;
    output.driveMode = ByteUtil.bitValue(bytes[offset], 0, 4);
    output.ridingMode = ByteUtil.bitValue(bytes[offset], 4, 4);
    offset++;
    output.comfortableDriveModePedalSensitivity = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.classicDriveModePedalSensitivity = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;

    offset++;
    output.lightEffectMode = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    offset++;
    offset++;
    offset++;
    offset++;
    offset++;
    offset += 2;
    output.speedingFeedback = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;
    output.brakingFeedback = ByteUtil.bytes2Uint8Le(bytes, offset);
    offset++;

    offset += 6; // 保留6byte

    output.soundState = ByteUtil.bitValue(bytes[offset], 0, 1);
    // 跳过1bit
    output.liftedState = ByteUtil.bitValue(bytes[offset], 2, 1);
    // 跳过2bit
    output.lockState = ByteUtil.bitValue(bytes[offset], 5, 1);
    output.transportModeState = ByteUtil.bitValue(bytes[offset], 6, 1);
    output.loadDetect = ByteUtil.bitValue(bytes[offset], 7, 1);
    offset++;
    output.noLoadDetect = ByteUtil.bitValue(bytes[offset], 0, 1);
    output.lowBatteryMode = ByteUtil.bitValue(bytes[offset], 1, 1);
    // 跳过4bit
    output.remainderRangeEstimate = ByteUtil.bitValue(bytes[offset], 6, 1);
    output.assistBalance = ByteUtil.bitValue(bytes[offset], 7, 1);
    offset++;
    output.speedingBrakingFeedbackState = ByteUtil.bitValue(bytes[offset], 0, 1);
  }

  @override
  int length() {
    return 42;
  }
}
